package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.RedisConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.service.ValidateCodeService;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.concurrent.TimeUnit;

/**
 * @author wgq
 * @date 2019-10-17 19:38
 * 手机验证码发送  = >  业务层
 */
@Service(interfaceClass = ValidateCodeService.class)
@Transactional
public class ValidateCodeServiceImpl implements ValidateCodeService {

    /*@Autowired
    private JedisPool jedisPool;*/

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired(required = false)
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        RedisSerializer stringSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringSerializer);
        redisTemplate.setValueSerializer(stringSerializer);
        redisTemplate.setHashKeySerializer(stringSerializer);
        redisTemplate.setHashValueSerializer(stringSerializer);
        this.redisTemplate = redisTemplate;
    }

    /**
     * 预约手机验证码发送  --  生成验证码 --> 调用阿里云发送  --> 验证码存Redis。
     *
     * @param telephone
     */
    @Override
    public void sendForOrder(String telephone) throws Exception {
        String code = ValidateCodeUtils.generateValidateCodeStr(4);
        SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE, telephone, code);
        System.out.println("发送的预约验证码为：" + code);
        //这里使用Jedis进行 存储并设置 超时时间
        //jedisPool.getResource().setex(telePhone+ RedisMessageConstant.SENDTYPE_ORDER,5*60,code);
        //这里改用 RedisTemplate 来进行 缓存存储

        //RedisTemplate 字符串类型存储与获取
        redisTemplate.opsForValue().set(telephone + RedisMessageConstant.SENDTYPE_ORDER, code);
        redisTemplate.expire(telephone + RedisMessageConstant.SENDTYPE_ORDER, 30 * 24 * 60, TimeUnit.MINUTES);
        //redisTemplate.opsForValue().set(telePhone+RedisMessageConstant.SENDTYPE_ORDER, code, 5, TimeUnit.MINUTES);

        //HashMap格式  的过期时间设置
        //redisTemplate.opsForHash().put("Sms_Order", telePhone+RedisMessageConstant.SENDTYPE_ORDER,code);
        //redisTemplate.expire("Sms_Order", 60*24*60, TimeUnit.MINUTES);
    }

    /**
     * 登录手机验证码发送  --  生成验证码 --> 调用阿里云发送  --> 验证码存Redis。
     * @param telephone
     * @throws Exception
     */
    @Override
    public void sendForLogin(String telephone) throws Exception {
        String code = ValidateCodeUtils.generateValidateCodeStr(6);
        SMSUtils.sendShortMessage(SMSUtils.LOGIN_CODE, telephone, code);
        System.out.println("发送的登录验证码为：" + code);
        //此处使用RedisTemplate进行字符串类型的存储和获取
        redisTemplate.opsForValue().set(telephone + RedisMessageConstant.SENDTYPE_LOGIN, code, 30 * 24 * 60, TimeUnit.MINUTES);
    }
}
